package com.ls.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.domain.Confirm;
import com.ls.domain.Message;
import com.ls.domain.Student;
import com.ls.service.ConfirmService;
import com.ls.service.MessageService;
import com.ls.service.StudentService;
import com.ls.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private ConfirmService confirmService;

    //===消息表    增删改查
    //添加消息 实体（通过班级把学号和消息号添加到确认表）
    @PostMapping("/save")
    public boolean save(@RequestBody Message message){
        String mClass = message.getMessageClass();
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper.eq(Student::getStudentClass,mClass);
        List<Student> studentsExist = studentService.list(studentLambdaQueryWrapper);
        messageService.save(message);
        Integer mid = message.getId();
        if (mClass.equals("全体学生")){
            List<Student> students = studentService.list();
            for (Student student:students) {
                Integer sid = student.getId();
                Confirm cf = new Confirm();
                cf.setSid(sid);
                cf.setMid(mid);
                confirmService.save(cf);
            }
        } else {
            List<Student> students = studentService.list(studentLambdaQueryWrapper);
            for (Student student:students) {
                Integer sid = student.getId();
                Confirm cf = new Confirm();
                cf.setSid(sid);
                cf.setMid(mid);
                confirmService.save(cf);
            }
        }
        return true;
    }

    //删除消息 id
    @DeleteMapping("/deleteById/{id}")
    public boolean deleteById(@PathVariable("id") Integer id){
        return messageService.removeById(id);
    }

    //批量删除消息 ids
    @PostMapping("/deleteByIds")
    public boolean deleteByIds(@RequestBody List<Integer> ids){
        return messageService.removeByIds(ids);
    }

    //更改消息 id
    @PutMapping("/update/{id}")
    public boolean update(@RequestBody Message message){
        return messageService.updateById(message);
    }

    //查询消息 id
    @GetMapping("/selectById/{id}")
    public Message selectById(@PathVariable("id") Integer id){
        return messageService.getById(id);
    }

    //管理员查询所有消息
    @GetMapping("/selectAll")
    public Map<String, Object> selectAll(int currentPage, int pageSize){
        Page<Message> pageInfo = new Page<>(currentPage, pageSize);
        messageService.page(pageInfo);
        Map<String, Object> map = new HashMap<>();
        map.put("tableData", pageInfo.getRecords());
        map.put("totalCount", pageInfo.getTotal());
        return map;
    }

    //教师查询本班有的消息 教师班级
    @GetMapping("/selectAllTch")
    public Map<String, Object> selectAll(int currentPage, int pageSize, String teacherClass){
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getMessageClass, teacherClass);
        Page<Message> pageInfo = new Page<>(currentPage, pageSize);
        messageService.page(pageInfo,queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("tableData", pageInfo.getRecords());
        map.put("totalCount", pageInfo.getTotal());
        return map;
    }

    //管理员搜索消息 实体 消息标题、消息班级
    @PostMapping("/selectByConditions")
    public Map<String, Object> selectByConditions(int currentPage, int pageSize,@RequestBody Message message){
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Message::getMessageTitle,message.getMessageTitle())
                .like(Message::getMessageClass,message.getMessageClass());
        Page<Message> pageInfo = new Page<>(currentPage, pageSize);
        messageService.page(pageInfo,queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("tableData", pageInfo.getRecords());
        map.put("totalCount", pageInfo.getTotal());
        return map;
    }

    //教师搜索本班消息 实体、教师班级
    @PostMapping("/selectByConditionsTch")
    public Map<String, Object> selectByConditionsTch(int currentPage, int pageSize, String teacherClass, @RequestBody Message message){
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Message::getMessageTitle,message.getMessageTitle())
                .like(Message::getMessageClass,teacherClass);
        Page<Message> pageInfo = new Page<>(currentPage, pageSize);
        messageService.page(pageInfo,queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("tableData", pageInfo.getRecords());
        map.put("totalCount", pageInfo.getTotal());
        return map;
    }

    //===确认表
    //学生修改自己的是否确认 实体、页面中学生id、点击中报告id
    @PutMapping("/updateStu")
    public boolean updateStu(Integer sid, Integer mid){
        LambdaQueryWrapper<Confirm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Confirm::getSid,sid)
                .eq(Confirm::getMid,mid);
        Confirm confirm = confirmService.getOne(queryWrapper);
        confirm.setMessageConfirm(1);
        return confirmService.update(confirm,queryWrapper);
    }

    //学生批量修改自己的是否确认 实体、页面中学生id、点击中报告id
    @PostMapping("/updateStus")
    public boolean updateStus(Integer sid,@RequestBody List<Integer> mids){
        for (Integer mid:mids) {
            LambdaQueryWrapper<Confirm> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Confirm::getSid,sid)
                    .eq(Confirm::getMid,mid);
            Confirm confirm = confirmService.getOne(queryWrapper);
            confirm.setMessageConfirm(1);
            confirmService.update(confirm,queryWrapper);
        }

        return true;
    }

    //学生查询本人消息 注解sql: sid、当前页面、页面大小
    @GetMapping("/selectBySid")
    public Map<String, Object> selectBySid(Integer sid, int currentPage, int pageSize){
        int startIndex = (currentPage-1)*pageSize;
        List<Message> messageList = confirmService.selectBySid(sid,startIndex,pageSize);
        int count = confirmService.selectBySidCount(sid);
        Map<String, Object> map = new HashMap<>();
        map.put("tableData", messageList);
        map.put("totalCount", count);
        return map;
    }

    //学生搜索自己消息  注解sql：sid、消息标题、当前页面、页面大小
    @PostMapping("/selectBySidConditions")
    public Map<String, Object> selectBySidConditions(Integer sid, String messageTitle,int currentPage, int pageSize){
        int startIndex = (currentPage-1)*pageSize;
        List<Message> messageList = confirmService.selectBySidConditions(sid,messageTitle,startIndex,pageSize);
        int count = confirmService.selectBySidConditionsCount(sid,messageTitle);
        Map<String, Object> map = new HashMap<>();
        map.put("tableData", messageList);
        map.put("totalCount", count);
        return map;
    }
}
